package com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator;

import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.ConfigurationTypeEnum;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.ConfigurationTypeEnumInterface;
import com.personal.gateway.utils.StringTemplateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author BigHead
 * @date 2022/1/11 10:05
 * @description WebObjectGeneratorString
 */
public class WebObjectGeneratorEnum extends AbstractWebObjectGenerator<Enum<? extends ConfigurationTypeEnumInterface>, ConfigurationTypeEnum> {

    private static final String PLEASE_CHOICE = "请选择 ";

    // 选择器

    private static final String OPTION_VALUE = "OPTION_VALUE";
    private static final String OPTION_NAME = "OPTION_NAME";
    private static final String OPTION_ELEMENT_TEMPLATE = "<option value=\"${OPTION_VALUE}\">${OPTION_NAME}</option>";

    // 基础输入组件

    private static final String WEB_ID = "WEB_ID";
    private static final String OPTION_ELEMENT = "OPTION_ELEMENT";
    private static final String SELECT_OPTION_ELEMENT = "" +
            "<select id=\"${WEB_ID}\">\n" +
            "${OPTION_ELEMENT}\n" +
            "</select>";

    @Override
    protected String getInputElement(ConfigurationOption<Enum<? extends ConfigurationTypeEnumInterface>, ConfigurationTypeEnum> configurationOption) {
        ConfigurationTypeEnumInterface selected = null == configurationOption.getConfigurationType().getValue() ? null : (ConfigurationTypeEnumInterface) configurationOption.getConfigurationType().getValue();
        List<ConfigurationTypeEnumInterface> configurationTypeEnumInterfaceList = new ArrayList<>();
        if (null == configurationOption.getAntecedentConfigurationOption()) {
            for (Enum<? extends ConfigurationTypeEnumInterface> configurationTypeEnumInterface : configurationOption.getConfigurationType().getExampleValue()) {
                configurationTypeEnumInterfaceList.add(
                        (ConfigurationTypeEnumInterface) configurationTypeEnumInterface
                );
            }
        } else {
            for (Enum<? extends ConfigurationTypeEnumInterface> configurationTypeEnumInterface : configurationOption.getConfigurationType().getExampleValue(configurationOption.getAntecedentConfigurationOption())) {
                configurationTypeEnumInterfaceList.add(
                        (ConfigurationTypeEnumInterface) configurationTypeEnumInterface
                );
            }
        }
        // 拼装样例数据
        List<String> selectOptionList = new ArrayList<>();
        if (null == selected) {
            makeNoSelectOption(configurationTypeEnumInterfaceList, selectOptionList);
        } else {
            makeSelectOption(configurationTypeEnumInterfaceList, selectOptionList, selected);
        }


        return StringTemplateUtils.render(
                SELECT_OPTION_ELEMENT,
                new HashMap<String, String>(2) {{
                    put(WEB_ID, webElementIdRecode(configurationOption.getConfigurationKey().getName()));
                    put(OPTION_ELEMENT, String.join("\n", selectOptionList));
                }}
        );
    }

    private void makeNoSelectOption(List<ConfigurationTypeEnumInterface> configurationTypeEnumInterfaceList, List<String> selectOptionList) {
        String exampleValue = PLEASE_CHOICE + configurationTypeEnumInterfaceList
                .stream()
                .map(ConfigurationTypeEnumInterface::getSelectValue)
                .collect(Collectors.joining(","));

        selectOptionList.add(
                StringTemplateUtils.render(
                        OPTION_ELEMENT_TEMPLATE,
                        new HashMap<String, String>(2) {{
                            put(OPTION_VALUE, "");
                            put(OPTION_NAME, exampleValue);
                        }}
                )
        );
        // 拼装选择对象
        selectOptionList.addAll(
                configurationTypeEnumInterfaceList
                        .stream()
                        .map(configurationTypeEnumInterface ->
                                StringTemplateUtils.render(
                                        OPTION_ELEMENT_TEMPLATE,
                                        new HashMap<String, String>(2) {{
                                            put(OPTION_VALUE, configurationTypeEnumInterface.getShowValue());
                                            put(OPTION_NAME, configurationTypeEnumInterface.getSelectValue());
                                        }}
                                ))
                        .collect(Collectors.toList())
        );
    }

    private void makeSelectOption(List<ConfigurationTypeEnumInterface> configurationTypeEnumInterfaceList, List<String> selectOptionList, ConfigurationTypeEnumInterface selected) {
        // 拼装已选择的对象
        selectOptionList.add(
                StringTemplateUtils.render(
                        OPTION_ELEMENT_TEMPLATE,
                        new HashMap<String, String>(2) {{
                            put(OPTION_VALUE, selected.getShowValue());
                            put(OPTION_NAME, selected.getSelectValue());
                        }}
                )
        );
        // 拼装选择对象
        selectOptionList.addAll(
                configurationTypeEnumInterfaceList
                        .stream()
                        .filter(configurationTypeEnumInterface -> !configurationTypeEnumInterface.getShowValue().equals(selected.getShowValue()) && !configurationTypeEnumInterface.getSelectValue().equals(selected.getSelectValue()))
                        .map(configurationTypeEnumInterface ->
                                StringTemplateUtils.render(
                                        OPTION_ELEMENT_TEMPLATE,
                                        new HashMap<String, String>(2) {{
                                            put(OPTION_VALUE, configurationTypeEnumInterface.getShowValue());
                                            put(OPTION_NAME, configurationTypeEnumInterface.getSelectValue());
                                        }}
                                ))
                        .collect(Collectors.toList())
        );
    }
}
